-- Author  : Kurapica
-- Create Date : 2010/06/27

do
	IGAS:NewAddon("IGAS_Studio")

	local form = CreateFrame(Widget.AdvanceForm, "IGAS_Studio_SetPointTool", coreF)
	form.Caption = L["Point Settings"]
	form.btnUp.Visible = false
	form.btnDown.Visible = false

	local lst = form.List
	local dtg = form.DataGrid
	local _Frame = nil

	local _DirRelativePoint = {
		"TOP",
		"BOTTOM",
		"LEFT",
		"RIGHT",
		"TOPLEFT",
		"TOPRIGHT",
		"BOTTOMLEFT",
		"BOTTOMRIGHT",
	}

	local _DirPoint = {	}

	local _RelativeFrames = {}
	local _RelativeFrameNames = {}

	form:RegisterEvent("IGAS_STUDIO_SET_POINT")

	dtg.RowCount = 2
	dtg.Cells(1, 1).Text = L["RelativeFrame"]
	dtg.Cells(2, 1).Text = L["RelativePoint"]

	local function CheckRelative(frame, relative, parent)
		local relativeTo
		for i = 1, relative:GetNumPoints() do
			_, relativeTo = relative:GetPoint(i)
			
			if _FrameInfoList[relativeTo] and relativeTo ~= parent and _FrameInfoList[relativeTo].Parent == parent then
				if relativeTo == frame then
					return false
				end
				
				if not CheckRelative(frame, relativeTo, parent) then
					return false
				end
			end
		end
		
		return true
	end

	local function AddPoint(point)
		if point and strtrim(point) ~= "" then
			lst:AddItem({point = point}, point)
		end
	end
	
	local function GetPos(frame, point)
		local x, y = frame:GetCenter()
		
		if strfind(point, "TOP") then
			y = frame:GetTop()
		elseif strfind(point, "BOTTOM") then
			y = frame:GetBottom()
		end
		
		if strfind(point, "LEFT") then
			x = frame:GetLeft()
		elseif strfind(point, "RIGHT") then
			x = frame:GetRight()
		end
		
		return x, y
	end
	
	function form:IGAS_STUDIO_SET_POINT(frame)		
		local parent = _FrameInfoList[frame].Parent
		
		_Frame = frame
		
		tinsert(_RelativeFrames, parent)
		tinsert(_RelativeFrameNames, _FrameInfoList[parent].Name)

		for frm, info in pairs(_FrameInfoList) do
			if frm ~= frame and info.Parent == parent and not frm:IsObjectType("VirtualUIObject") then
				if CheckRelative(frame, frm, parent) then
					tinsert(_RelativeFrames, frm)
					tinsert(_RelativeFrameNames, _FrameInfoList[frm].Name)
				end
			end
		end
		
		for i = 1, frame:GetNumPoints() do
			local point, relativeTo, relativePoint, xOffset, yOffset = frame:GetPoint(i)
			local detail = {point = point,
							relativeTo = relativeTo,
							relativePoint = relativePoint,
							xOffset = xOffset,
							yOffset = yOffset,}
			
			lst:AddItem(detail, point)
		end
		
		self.Visible = true
	end

	function lst:OnItemChoosed(detail, point)
		dtg.Detail = nil
		
		dtg.Cells(1, 2, 2, 2).CellType = "ComboBox"

		dtg.Cells(1, 2).Keys = _RelativeFrames
		dtg.Cells(1, 2).Items = _RelativeFrameNames
		
		dtg.Cells(2, 2).Keys = _DirRelativePoint
		dtg.Cells(2, 2).Items = _DirRelativePoint
		
		dtg.Cells(1, 2).Value = detail and detail.relativeTo
		dtg.Cells(2, 2).Value = detail and detail.relativePoint
		
		dtg.Detail = detail
	end

	function dtg:OnCellValueChanged(row, column, value)
		if column == 2 and self.Detail then
			Log(1, "[SetPointGuide][CellValueChanged]["..self.Detail.point.."]"..column)
			if row == 1 then
				self.Detail.relativeTo = self:GetCellValue(1, 2)
			elseif row == 2 then
				self.Detail.relativePoint = self:GetCellValue(2, 2)
			end
		end
	end
	
	function form:OnOkayClick()
		-- Save Data
		_Frame:ClearAllPoints()
		
		for _, detail in pairs(lst.Keys) do
			if detail.relativeTo and detail.relativePoint and strtrim(detail.relativePoint) ~= "" then
				local x, y = GetPos(_Frame, detail.point)
				local rx, ry = GetPos(detail.relativeTo, detail.relativePoint)
				
				Log(1, "[SetPointGuide]["..detail.point.."][".._FrameInfoList[detail.relativeTo].Name.."]["..detail.relativePoint.."]")
				_Frame:SetPoint(detail.point, detail.relativeTo, detail.relativePoint, x - rx, y - ry)
			end
		end
		
		self.Visible = false
	end
	
	function form:OnAddClick()
		wipe(_DirPoint)
		
		for _, i in pairs(_DirRelativePoint) do
			_DirPoint[i] = i
		end
		
		for _, i in pairs(lst.Items) do
			_DirPoint[i] = nil
		end
		
		if next(_DirPoint) then
			return IGAS:MsgBox(L["Select an anchor point"], "ic", AddPoint, _DirPoint)
		end
	end

	function form:OnRemoveClick()
		if lst:GetSelectedItemIndex() > 0 then
			dtg.Detail = nil
			
			-- do clear
			dtg.Cells(1, 2, 2, 2).CellType = "Label"
			dtg.Cells(1, 2, 2, 2).Text = ""			
		
			lst:RemoveItem(lst:GetSelectedItemValue())
		end
	end

	function form:OnHide()
		_Frame = nil
		dtg.Detail = nil
		
		-- do clear
		dtg.Cells(1, 2, 2, 2).CellType = "Label"
		dtg.Cells(1, 2, 2, 2).Text = ""

		wipe(_DirPoint)
		wipe(_RelativeFrames)
		wipe(_RelativeFrameNames)

		lst:Clear()
	end
end